﻿namespace ShareDef
{
    using System;
    using System.Data;
    using System.IO;
    using System.Net;
    using System.Security.Cryptography;
    using System.Text;

    public class Util
    {
        public static long ConvertIPToUInt(string ip)
        {
            return (long) BitConverter.ToUInt32(IPAddress.Parse(ip).GetAddressBytes(), 0);
        }

        public static string ConvertLongToIP(long IPValue)
        {
            string str = "";
            for (int i = 4; i > 0; i--)
            {
                long num2 = (long) Math.Pow(256.0, (double) (i - 1));
                int num3 = (int) (IPValue / num2);
                IPValue -= num3 * num2;
                if (str != "")
                {
                    str = str + "." + num3.ToString();
                }
                else
                {
                    str = num3.ToString();
                }
            }
            return str;
        }

        public static DataTable CreatePagedData(DataTable dt, string start, string limit)
        {
            DataTable table = new DataTable();
            foreach (DataColumn column in dt.Columns)
            {
                table.Columns.Add(column.ColumnName, column.DataType);
            }
            if (dt.Rows.Count != 0)
            {
                int num = Convert.ToInt32(start);
                int num2 = Convert.ToInt32(limit);
                if (num2 > (dt.Rows.Count - num))
                {
                    num2 = dt.Rows.Count - num;
                }
                DataRow row = null;
                for (int i = 0; i < num2; i++)
                {
                    row = table.NewRow();
                    row.ItemArray = dt.Rows[num + i].ItemArray;
                    table.Rows.Add(row);
                }
            }
            return table;
        }

        public static DataTable CreateUserTable(string tableName, string[] colNames)
        {
            DataTable table = new DataTable();
            for (int i = 0; i < colNames.Length; i++)
            {
                DataColumn column = new DataColumn {
                    DataType = Type.GetType("System.String"),
                    ColumnName = colNames[i]
                };
                table.Columns.Add(column);
            }
            table.TableName = tableName;
            return table;
        }

        public static uint DateTimeToTimeStamp(DateTime datetime)
        {
            DateTime time = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(0x7b2, 1, 1));
            TimeSpan span = (TimeSpan) (datetime - time);
            return (uint) span.TotalSeconds;
        }

        public static string DesBase64Decrypt(string input, Encoding l_encoding)
        {
            try
            {
                DES des = DES.Create();
                des.Mode = CipherMode.ECB;
                byte[] rgbKey = new byte[] { 0x38, 50, 0x37, 0x38, 0x38, 0x37, 0x31, 0x31 };
                byte[] rgbIV = new byte[8];
                ICryptoTransform transform = des.CreateDecryptor(rgbKey, rgbIV);
                byte[] buffer = Convert.FromBase64String(input);
                MemoryStream stream = new MemoryStream();
                CryptoStream stream2 = new CryptoStream(stream, transform, CryptoStreamMode.Write);
                stream2.Write(buffer, 0, buffer.Length);
                stream2.FlushFinalBlock();
                stream2.Close();
                return l_encoding.GetString(stream.ToArray());
            }
            catch (Exception)
            {
            }
            return input;
        }

        public static string DesBase64Encrypt(string input, Encoding l_encoding)
        {
            DES des = DES.Create();
            des.Mode = CipherMode.ECB;
            byte[] rgbKey = new byte[] { 0x38, 50, 0x37, 0x38, 0x38, 0x37, 0x31, 0x31 };
            byte[] rgbIV = new byte[8];
            ICryptoTransform transform = des.CreateEncryptor(rgbKey, rgbIV);
            byte[] bytes = l_encoding.GetBytes(input);
            MemoryStream stream = new MemoryStream();
            CryptoStream stream2 = new CryptoStream(stream, transform, CryptoStreamMode.Write);
            stream2.Write(bytes, 0, bytes.Length);
            stream2.FlushFinalBlock();
            stream2.Close();
            byte[] buffer4 = stream.ToArray();
            for (int i = 0; i < buffer4.Length; i++)
            {
                Console.Write(buffer4[i].ToString() + " ");
            }
            Console.WriteLine();
            return Convert.ToBase64String(stream.ToArray());
        }
        public static string MD5Encrypt(string input)
        {
            Encoding encoding = Encoding.UTF8;
            return BitConverter.ToString(new MD5CryptoServiceProvider().ComputeHash(encoding.GetBytes(input))).Replace("-", "");
        }

        public static string SecondToTime(uint second)
        {
            string str = "";
            if ((second > 0) && (second < 0x15180))
            {
                uint num = second / 0xe10;
                uint num2 = (second % 0xe10) / 60;
                uint num3 = second % 60;
                str = string.Format("{0:d2}:{1:d2}:{2:d2}", num, num2, num3);
            }
            return str;
        }

        public static DateTime TimeStampToDateTime(string timestamp)
        {
            DateTime time = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(0x7b2, 1, 1));
            long ticks = long.Parse(timestamp + "0000000");
            TimeSpan span = new TimeSpan(ticks);
            return time.Add(span);
        }

        public static string TimeStampToDateTimeString(uint timestamp)
        {
            DateTime time = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(0x7b2, 1, 1));
            long ticks = timestamp * 0x989680L;
            TimeSpan span = new TimeSpan(ticks);
            return time.Add(span).ToString("yyyy-MM-dd HH:mm:ss");
        }

        public static uint TimeToSecond(string timeString)
        {
            int num = 0;
            int result = 0;
            string[] strArray = timeString.Split(new char[] { ':' });
            if (strArray.Length != 3)
            {
                return 0;
            }
            result = 0;
            int.TryParse(strArray[0], out result);
            num += result * 0xe10;
            result = 0;
            int.TryParse(strArray[1], out result);
            num += result * 60;
            result = 0;
            int.TryParse(strArray[2], out result);
            num += result;
            return (uint) num;
        }
    }
}

